﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.Entities;
using System.Data.SqlClient;

namespace DAL.EntitiesDAL
{
    public class CuonSachDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_CuonSach");
        }

        public DataTable Select_themsachmuon()
        {
            return ExecuteQuery("sp_SelectAll_Themsachmuon");
        }

        public DataTable Select_Tungphieumuon(string stt_pm)
        {
            var sql = "sp_Select_Tungphieumuon '" + stt_pm + "'";
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_CuonSach {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (CuonSach)obj;
            var sql = "set dateformat dmy; execute sp_Insert_CuonSach '{0}', '{1}', '{2}' ";
            sql = string.Format(sql, o.MACUONSACH, o.MADAUSACH, o.MAKESACH);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (CuonSach)obj;
            var sql = "set dateformat dmy; execute sp_Update_CuonSach '{0}', '{1}', '{2}' ";
            sql = string.Format(sql, o.MACUONSACH, o.MADAUSACH, o.MAKESACH);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"Select * from tb_Cuonsach";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.CuonSach o)
        {
            string sql;
            if (o.MACUONSACH != "")
                sql = @"Select * from tb_Cuonsach Where MACUONSACH like '%" + o.MACUONSACH + "%' ";
            else 
                     sql = @"Select * from tb_Cuonsach Where MADAUSACH like '%" + o.MADAUSACH + "%' or MAKESACH like '%" + o.MAKESACH + "%' ";
                 
            return ExecuteQuery(sql);
        }

        public DataTable Select_Max()
        {
            var sql = @"Select Max(MACUONSACH) as Ma From tb_Cuonsach";
            return ExecuteQuery(sql);
        }


        #endregion

      
    }
}
